package com.nan.samsung.qrsan;

import java.util.ArrayList;

import org.apache.http.Header;
import org.json.JSONArray;
import org.json.JSONObject;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.framework.nan.asynchttp.AsyncHttpClient;
import com.android.framework.nan.asynchttp.JsonHttpResponseHandler;
import com.android.framework.nan.asynchttp.RequestParams;

public class MainActivity extends Activity {
	private final String TAG = getClass().getSimpleName();
	private static final int REQUEST_SCANNING_CODE = 20170722;

	private ListView mScanListView;
	private ArrayList<String> mScanList;
	private MyAdapter mAdapter;
	private ProgressDialog mProgress;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		setContentView(R.layout.activity_main);
		mScanList = new ArrayList<String>();
		mAdapter = new MyAdapter();
		mScanListView = (ListView) findViewById(R.id.lv_scans);
		mScanListView.setAdapter(mAdapter);

		mProgress = new ProgressDialog(this);
		mProgress.setTitle("Posting");
		mProgress.setCancelable(false);
		mProgress.setMessage("Post the information, please wait ...");
		super.onCreate(savedInstanceState);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
		if (REQUEST_SCANNING_CODE == requestCode && ZXingConstants.SCAN_REQULT_CODE == resultCode) {
			if (intent != null) {
				String result = intent.getStringExtra(ZXingConstants.SCAN_RESULT);
				Log.i(TAG, "scan result " + result);
				if (result != null && !result.isEmpty()) {
					mScanList.add(result);
					mAdapter.notifyDataSetChanged();
				}
			}
		} else {
			Log.e(TAG, "not matched requestCode " + requestCode + " resultCode" + resultCode);
		}
		super.onActivityResult(requestCode, resultCode, intent);
	}

	public void onSetting(View view) {
		startActivity(new Intent(this, SettingsActivity.class));
	}

	public void onScan(View view) {
		startActivityForResult(new Intent(this, CaptureActivity.class), REQUEST_SCANNING_CODE);
	}

	public void onClear(View view) {
		mScanList.clear();
		mAdapter.notifyDataSetChanged();
	}

	public void onPost(View view) {
		SharedPreferences sp = getSharedPreferences(ZXingConstants.MY_SP, Context.MODE_PRIVATE);
		String ip = sp.getString(ZXingConstants.MY_IP, ZXingConstants.DEFAULT_IP);
		String func = sp.getString(ZXingConstants.MY_FUNC, ZXingConstants.DEFAULT_FUNC);

		RequestParams params = new RequestParams();
		params.add(func, mScanList.toString());

		Toast.makeText(this, "Url: " + ip + "\nFuncton: " + func + "\nParams: " + params.toString(), Toast.LENGTH_LONG)
				.show();

		mProgress.show();

		AsyncHttpClient client = new AsyncHttpClient();
		client.post(this, ip, params, new JsonHttpResponseHandler() {

			@Override
			public void onSuccess(int statusCode, Header[] headers, JSONArray response) {
				mProgress.dismiss();
				Toast.makeText(MainActivity.this, "Success: " + response.toString(), Toast.LENGTH_LONG).show();
				super.onSuccess(statusCode, headers, response);
			}

			@Override
			public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
				mProgress.dismiss();
				Toast.makeText(MainActivity.this, "Success: " + response, Toast.LENGTH_LONG).show();
				super.onSuccess(statusCode, headers, response);
			}

			@Override
			public void onSuccess(int statusCode, Header[] headers, String responseString) {
				mProgress.dismiss();
				Toast.makeText(MainActivity.this, "Success: " + responseString, Toast.LENGTH_LONG).show();
				super.onSuccess(statusCode, headers, responseString);
			}

			@Override
			public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONObject errorResponse) {
				mProgress.dismiss();
				Toast.makeText(MainActivity.this, "Error: " + errorResponse, Toast.LENGTH_LONG).show();
				super.onFailure(statusCode, headers, throwable, errorResponse);
			}

			@Override
			public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONArray errorResponse) {
				mProgress.dismiss();
				Toast.makeText(MainActivity.this, "Error: " + errorResponse.toString(), Toast.LENGTH_LONG).show();
				super.onFailure(statusCode, headers, throwable, errorResponse);
			}

			@Override
			public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
				mProgress.dismiss();
				Toast.makeText(MainActivity.this, "Error: " + responseString, Toast.LENGTH_LONG).show();
				super.onFailure(statusCode, headers, responseString, throwable);
			}
		});
	}

	public class MyAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			return mScanList.size();
		}

		@Override
		public Object getItem(int position) {
			return mScanList.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			if (convertView == null) {
				convertView = LayoutInflater.from(MainActivity.this).inflate(R.layout.item_scan, null);
			}

			TextView tv = (TextView) convertView.findViewById(R.id.tv_item);
			tv.setText(mScanList.get(position));
			return convertView;
		}
	}
}
